home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 242_01 / a611util.c < prev    next >
Text File  |  1989-01-11  |  15KB  |  550 lines

  1. /*
  2.     HEADER:        CUG242;
  3.     TITLE:        68HC11 Cross-Assembler (Portable);
  4.     FILENAME:    A611UTIL.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    A611.H
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.              68HC11 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1985,1987 William C. Colley, III
  15.  
  16. This module contains the following utility packages:
  17.  
  18.     1)  symbol table building and searching
  19.  
  20.     2)  opcode and operator table searching
  21.  
  22.     3)  listing file output
  23.  
  24.     4)  hex file output
  25.  
  26.     5)  error flagging
  27. */
  28.  
  29. /*  Get global goodies:  */
  30.  
  31. #include "a611.h"
  32.  
  33. /*  Make sure that MSDOS compilers using the large memory model know    */
  34. /*  that calloc() returns pointer to char as an MSDOS far pointer is    */
  35. /*  NOT compatible with the int type as is usually the case.        */
  36.  
  37. char *calloc();
  38.  
  39. /*  Get access to global mailboxes defined in A68.C:            */
  40.  
  41. extern char errcode, line[], title[];
  42. extern int eject, listhex;
  43. extern unsigned address, bytes, errors, listleft, obj[], pagelen;
  44.  
  45. /*  The symbol table is a binary tree of variable-length blocks drawn    */
  46. /*  from the heap with the calloc() function.  The root pointer lives    */
  47. /*  here:                                */
  48.  
  49. static SYMBOL *sroot = NULL;
  50.  
  51. /*  Add new symbol to symbol table.  Returns pointer to symbol even if    */
  52. /*  the symbol already exists.  If there's not enough memory to store    */
  53. /*  the new symbol, a fatal error occurs.                */
  54.  
  55. SYMBOL *new_symbol(nam)
  56. char *nam;
  57. {
  58.     SCRATCH int i;
  59.     SCRATCH SYMBOL **p, *q;
  60.     void fatal_error();
  61.  
  62.     for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
  63.     p = i < 0 ? &(q -> left) : &(q -> right);
  64.     if (!q) {
  65.     if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
  66.         fatal_error(SYMBOLS);
  67.     strcpy(q -> sname,nam);
  68.     }
  69.     return q;
  70. }
  71.  
  72. /*  Look up symbol in symbol table.  Returns pointer to symbol or NULL    */
  73. /*  if symbol not found.                        */
  74.  
  75. SYMBOL *find_symbol(nam)
  76. char *nam;
  77. {
  78.     SCRATCH int i;
  79.     SCRATCH SYMBOL *p;
  80.  
  81.     for (p = sroot; p && (i = strcmp(nam,p -> sname));
  82.     p = i < 0 ? p -> left : p -> right);
  83.     return p;
  84. }
  85.  
  86. /*  Opcode table search routine.  This routine pats down the opcode    */
  87. /*  table for a given opcode and returns either a pointer to it or    */
  88. /*  NULL if the opcode doesn't exist.                    */
  89.  
  90. OPCODE *find_code(nam)
  91. char *nam;
  92. {
  93.     OPCODE *bsearch();
  94.  
  95.     static OPCODE opctbl[] = {
  96.     { 0,                        0x1b,    "ABA"    },
  97.     { 0,                        0x3a,    "ABX"    },
  98.     { 2,                        0x3a,    "ABY"    },
  99.     { IMMED + DIR + EXT + INDX + 0,            0x89,    "ADCA"    },
  100.     { IMMED + DIR + EXT + INDX + 0,            0xc9,    "ADCB"    },
  101.     { IMMED + DIR + EXT + INDX + 0,            0x8b,    "ADDA"    },
  102.     { IMMED + DIR + EXT + INDX + 0,            0xcb,    "ADDB"    },
  103.     { IMM_16 + IMMED + DIR + EXT + INDX + 0,    0xc3,    "ADDD"    },
  104.     { IMMED + DIR + EXT + INDX + 0,            0x84,    "ANDA"    },
  105.     { IMMED + DIR + EXT + INDX + 0,            0xc4,    "ANDB"    },
  106.     { EXT + INDX + 0,                0x48,    "ASL"    },
  107.     { 0,                        0x48,    "ASLA"    },
  108.     { 0,                        0x58,    "ASLB"    },
  109.     { 0,                        0x05,    "ASLD"    },
  110.     { EXT + INDX + 0,                0x47,    "ASR"    },
  111.     { 0,                        0x47,    "ASRA"    },
  112.     { 0,                        0x57,    "ASRB"    },
  113.     { REL + 0,                    0x24,    "BCC"    },
  114.     { MASK + DIR + INDX + 0,            0x15,    "BCLR"    },
  115.     { REL + 0,                    0x25,    "BCS"    },
  116.     { REL + 0,                    0x27,    "BEQ"    },
  117.     { REL + 0,                    0x2c,    "BGE"    },
  118.     { REL + 0,                    0x2e,    "BGT"    },
  119.     { REL + 0,                    0x22,    "BHI"    },
  120.     { REL + 0,                    0x24,    "BHS"    },
  121.     { IMMED + DIR + EXT + INDX + 0,            0x85,    "BITA"    },
  122.     { IMMED + DIR + EXT + INDX + 0,            0xc5,    "BITB"    },
  123.     { REL + 0,                    0x2f,    "BLE"    },
  124.     { REL + 0,                    0x25,    "BLO"    },
  125.     { REL + 0,                    0x23,    "BLS"    },
  126.     { REL + 0,                    0x2d,    "BLT"    },
  127.     { REL + 0,                    0x2b,    "BMI"    },
  128.     { REL + 0,                    0x26,    "BNE"    },
  129.     { REL + 0,                    0x2a,    "BPL"    },
  130.     { REL + 0,                    0x20,    "BRA"    },
  131.     { REL + MASK + DIR + INDX + 0,            0x13,    "BRCLR"    },
  132.     { REL + 0,                    0x21,    "BRN"    },
  133.     { REL + MASK + DIR + INDX + 0,            0x12,    "BRSET"    },
  134.     { MASK + DIR + INDX + 0,            0x14,    "BSET"    },
  135.     { REL + 0,                    0x8d,    "BSR"    },
  136.     { REL + 0,                    0x28,    "BVC"    },
  137.     { REL + 0,                    0x29,    "BVS"    },
  138.     { 0,                        0x11,    "CBA"    },
  139.     { 0,                        0x0c,    "CLC"    },
  140.     { 0,                        0x0e,    "CLI"    },
  141.     { EXT + INDX + 0,                0x4f,    "CLR"    },
  142.     { 0,                        0x4f,    "CLRA"    },
  143.     { 0,                        0x5f,    "CLRB"    },
  144.     { 0,                        0x0a,    "CLV"    },
  145.     { IMMED + DIR + EXT + INDX + 0,            0x81,    "CMPA"    },
  146.     { IMMED + DIR + EXT + INDX + 0,            0xc1,    "CMPB"    },
  147.     { EXT + INDX + 0,                0x43,    "COM"    },
  148.     { 0,                        0x43,    "COMA"    },
  149.     { 0,                        0x53,    "COMB"    },
  150.     { IMM_16 + IMMED + DIR + EXT + INDX + 3,    0x83,    "CPD"    },
  151.     { IMM_16 + IMMED + DIR + EXT + INDX + 1,    0x8c,    "CPX"    },
  152.     { IMM_16 + IMMED + DIR + EXT + INDX + 2,    0x8c,    "CPY"    },
  153.     { 0,                        0x19,    "DAA"    },
  154.     { EXT + INDX + 0,                0x4a,    "DEC"    },
  155.     { 0,                        0x4a,    "DECA"    },
  156.     { 0,                        0x5a,    "DECB"    },
  157.     { 0,                        0x34,    "DES"    },
  158.     { 0,                        0x09,    "DEX"    },
  159.     { 2,                        0x09,    "DEY"    },
  160.     { PSEUDO + ISIF,                ELSE,    "ELSE"    },
  161.     { PSEUDO,                    END,    "END"    },
  162.     { PSEUDO + ISIF,                ENDIF,    "ENDIF"    },
  163.     { IMMED + DIR + EXT + INDX + 0,            0x88,    "EORA"    },
  164.     { IMMED + DIR + EXT + INDX + 0,            0xc8,    "EORB"    },
  165.     { PSEUDO,                    EQU,    "EQU"    },
  166.     { PSEUDO,                    FCB,    "FCB"    },
  167.     { PSEUDO,                    FCC,    "FCC"    },
  168.     { PSEUDO,                    FDB,    "FDB"    },
  169.     { 0,                        0x03,    "FDIV"    },
  170.     { 0,                        0x02,    "IDIV"    },
  171.     { PSEUDO + ISIF,                IF,    "IF"    },
  172.     { EXT + INDX + 0,                0x4c,    "INC"    },
  173.     { 0,                        0x4c,    "INCA"    },
  174.     { 0,                        0x5c,    "INCB"    },
  175.     { PSEUDO,                    INCL,    "INCL"    },
  176.     { 0,                        0x31,    "INS"    },
  177.     { 0,                        0x08,    "INX"    },
  178.     { 2,                        0x08,    "INY"    },
  179.     { EXT + INDX + 0,                0x4e,    "JMP"    },
  180.     { DIR + EXT + INDX + 0,                0x8d,    "JSR"    },
  181.     { IMMED + DIR + EXT + INDX + 0,            0x86,    "LDAA"    },
  182.     { IMMED + DIR + EXT + INDX + 0,            0xc6,    "LDAB"    },
  183.     { IMM_16 + IMMED + DIR + EXT + INDX + 0,    0xcc,    "LDD"    },
  184.     { IMM_16 + IMMED + DIR + EXT + INDX + 0,    0x8e,    "LDS"    },
  185.     { IMM_16 + IMMED + DIR + EXT + INDX + 1,    0xce,    "LDX"    },
  186.     { IMM_16 + IMMED + DIR + EXT + INDX + 2,    0xce,    "LDY"    },
  187.     { EXT + INDX + 0,                0x48,    "LSL"    },
  188.     { 0,                        0x48,    "LSLA"    },
  189.     { 0,                        0x58,    "LSLB"    },
  190.     { 0,                        0x05,    "LSLD"    },
  191.     { EXT + INDX + 0,                0x44,    "LSR"    },
  192.     { 0,                        0x44,    "LSRA"    },
  193.     { 0,                        0x54,    "LSRB"    },
  194.     { 0,                        0x04,    "LSRD"    },
  195.     { 0,                        0x3d,    "MUL"    },
  196.     { EXT + INDX + 0,                0x40,    "NEG"    },
  197.     { 0,                        0x40,    "NEGA"    },
  198.     { 0,                        0x50,    "NEGB"    },
  199.     { 0,                        0x01,    "NOP"    },
  200.     { IMMED + DIR + EXT + INDX + 0,            0x8a,    "ORAA"    },
  201.     { IMMED + DIR + EXT + INDX + 0,            0xca,    "ORAB"    },
  202.     { PSEUDO,                    ORG,    "ORG"    },
  203.     { PSEUDO,                    PAGE,    "PAGE"    },
  204.     { 0,                        0x36,    "PSHA"    },
  205.     { 0,                        0x37,    "PSHB"    },
  206.     { 0,                        0x3c,    "PSHX"    },
  207.     { 2,                        0x3c,    "PSHY"    },
  208.     { 0,                        0x32,    "PULA"    },
  209.     { 0,                        0x33,    "PULB"    },
  210.     { 0,                        0x38,    "PULX"    },
  211.     { 2,                        0x38,    "PULY"    },
  212.     { PSEUDO,                    RMB,    "RMB"    },
  213.     { EXT + INDX + 0,                0x49,    "ROL"    },
  214.     { 0,                        0x49,    "ROLA"    },
  215.     { 0,                        0x59,    "ROLB"    },
  216.     { EXT + INDX + 0,                0x46,    "ROR"    },
  217.     { 0,                        0x46,    "RORA"    },
  218.     { 0,                        0x56,    "RORB"    },
  219.     { 0,                        0x3b,    "RTI"    },
  220.     { 0,                        0x39,    "RTS"    },
  221.     { 0,                        0x10,    "SBA"    },
  222.     { IMMED + DIR + EXT + INDX + 0,            0x82,    "SBCA"    },
  223.     { IMMED + DIR + EXT + INDX + 0,            0xc2,    "SBCB"    },
  224.     { 0,                        0x0d,    "SEC"    },
  225.     { 0,                        0x0f,    "SEI"    },
  226.     { PSEUDO,                    SET,    "SET"    },
  227.     { 0,                        0x0b,    "SEV"    },
  228.     { DIR + EXT + INDX + 0,                0x87,    "STAA"    },
  229.     { DIR + EXT + INDX + 0,                0xc7,    "STAB"    },
  230.     { DIR + EXT + INDX + 0,                0xcd,    "STD"    },
  231.     { 0,                        0xcf,    "STOP"    },
  232.     { DIR + EXT + INDX + 0,                0x8f,    "STS"    },
  233.     { DIR + EXT + INDX + 1,                0xcf,    "STX"    },
  234.     { DIR + EXT + INDX + 2,                0xcf,    "STY"    },
  235.     { IMMED + DIR + EXT + INDX + 0,            0x80,    "SUBA"    },
  236.     { IMMED + DIR + EXT + INDX + 0,            0xc0,    "SUBB"    },
  237.     { IMM_16 + IMMED + DIR + EXT + INDX + 0,    0x83,    "SUBD"    },
  238.     { 0,                        0x3f,    "SWI"    },
  239.     { 0,                        0x16,    "TAB"    },
  240.     { 0,                        0x06,    "TAP"    },
  241.     { 0,                        0x17,    "TBA"    },
  242.     { 0,                        0x00,    "TEST"    },
  243.     { PSEUDO,                    TITLE,    "TITLE"    },
  244.     { 0,                        0x07,    "TPA"    },
  245.     { EXT + INDX + 0,                0x4d,    "TST"    },
  246.     { 0,                        0x4d,    "TSTA"    },
  247.     { 0,                        0x5d,    "TSTB"    },
  248.     { 0,                        0x30,    "TSX"    },
  249.     { 2,                        0x30,    "TSY"    },
  250.     { 0,                        0x35,    "TXS"    },
  251.     { 2,                        0x35,    "TYS"    },
  252.     { 0,                        0x3e,    "WAI"    },
  253.     { 0,                        0x8f,    "XGDX"    },
  254.     { 2,                        0x8f,    "XGDY"    }
  255.     };
  256.  
  257.     return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
  258. }
  259.  
  260. /*  Operator table search routine.  This routine pats down the        */
  261. /*  operator